package com.cykj.pos.controller.api;

import com.alibaba.fastjson.JSONObject;
import com.cykj.common.core.controller.BaseController;
import com.cykj.common.core.domain.AjaxResult;
import com.cykj.pos.domain.SynchronizationDataDTO;

import com.cykj.pos.service.IBizSFTService;
import com.cykj.pos.service.IPosTransactionNoticeService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author 许晓龙
 * @date 2024/5/21 11:10
 */

@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/sft/notice" )
public class BizSFTController extends BaseController {

    private final IBizSFTService iBizSFTService;

    private final IPosTransactionNoticeService iPosTransactionNoticeService;


    @ApiOperation("盛付通机具入库通知")
    @PostMapping("/controller")
    public AjaxResult noticeOfEquipment(@RequestBody JSONObject dataJson){
        String businessType = dataJson.getString("businessType");
        // 业务类型：1-商户机具绑定通知；2-服务费收取通知；4-结算通知；5-提现通知；6-SIM 卡年费收取通知；7-机具入库通知
        if ("1".equals(businessType)){ // 业务类型：1-商户机具绑定通知；
           return iBizSFTService.sftMerchantInfoBind(dataJson);
        }else if ("2".equals(businessType)){ //2-服务费收取通知；
           return iBizSFTService.sftServiceFee(dataJson);
        } else if ("6".equals(businessType)){  //6-SIM 卡年费收取通知；
            return iBizSFTService.sftSimFee(dataJson);
        }else if ("3".equals(businessType)){  // 3 机具解绑
            return iBizSFTService.sftUnBind(dataJson);
        }
        return AjaxResult.error();

    }

    @ApiOperation("盛付通交易通知")
    @PostMapping("/transaction")
    public AjaxResult transactionNotification(@RequestBody SynchronizationDataDTO sy){
        System.out.println("------------------------------------------------------------------");
        System.out.println("--------盛付通交易通知：" + JSONObject.toJSONString(sy) + "---------");
        //转换时间格式
        String year = sy.getHost_date().substring(0, 4);
        String month = sy.getHost_date().substring(4, 6);
        String day = sy.getHost_date().substring(6, 8);
        String hour = sy.getHost_time().substring(0, 2);
        String minute = sy.getHost_time().substring(2, 4);
        String second = sy.getHost_time().substring(4, 6);
        String date = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
        sy.setHost_date(date);
        Map<String, String> transact = iPosTransactionNoticeService.transact(sy);
        String ret_msg = transact.get("ret_msg");
        return AjaxResult.success(ret_msg);
    }

}
